<script setup>

</script>

<template>
  <el-dialog v-model="dialogFormVisible" :title="(isModify ? '修改' : '新增') + '服务器'">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="服务器地址：" prop="address">
        <el-input v-model="form.address" autocomplete="off" style="width: 260px" clearable />
      </el-form-item>
      <el-form-item label="登录方式：" prop="login_type">
        <el-select v-model="form.login_type" placeholder="请选择" clearable>
          <el-option
              v-for="item in loginTypeList"
              :key="`login-type-form-select-${item.type}`"
              :label="item.name"
              :value="item.type"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联凭证：" prop="proof_id">
        <el-select v-model="form.proof_id" placeholder="请选择" clearable>
          <el-option
              v-for="item in proofList"
              :key="`login-type-form-select-${item.id}`"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="saveServer">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import EnumLoginType from "@/config/EnumLoginType"
import StoreProof from "@/store/StoreProof"
import StoreServer from "@/store/StoreServer"

export default {
  name: 'ServerForm',
  data() {
    return {
      dialogFormVisible: false,
      isModify: false,
      form: {
      },
      rules: {
        address: [
          { required: true, message: '请输入凭证名称', trigger: 'blur' }
        ],
        login_type: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        proof_id: [
          { required: true, message: '请输入管理密码', trigger: 'change' }
        ]
      },
      loginTypeList: EnumLoginType.valueList(),
      proofList: []
    }
  },

  methods: {
    /**
     * 显示窗口.
     * @param isModify {boolean}
     * @param serverData {Object}
     */
    show(isModify, serverData) {
      this.isModify = isModify
      this.loadProofList()
      this.form = Object.assign({}, serverData)
      this.dialogFormVisible = true
    },
    /**
     * 加载凭证列表.
     */
    loadProofList() {
      StoreProof.getAll({}, (data) => {
        this.proofList = data
      })
    },

    /**
     * 保存服务器.
     */
    saveServer() {
      this.$refs.ruleForm.validate((valid) => {
        if(valid) {
          const id = this.form.id
          const updateData = Object.assign(this.form, {})
          if(this.isModify) {
            delete updateData.id
            delete updateData.proof
            StoreServer.update(updateData, {id: id})
          } else {
            StoreServer.insert(updateData)
          }
          this.$emit('refresh', {})
          this.$message.success('保存成功')
          this.dialogFormVisible = false
        }
      })
    }
  },
  mounted() {
    StoreServer.init(this)
    StoreProof.init(this)
  }
}
</script>

<style scoped>

</style>